package com.example.amoxicilin.null_test.main;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.example.amoxicilin.null_test.R;

public class MainActivity extends AppCompatActivity {

    private CommunicationFragment communicationFragment;
    private FavorFragment favorFragment;
    private MeFragment meFragment;
    private String name;
    private int currentId= R.id.text_com;
    int usernumber;
    private TextView com, fav,me;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        Bundle bundle=getIntent().getExtras();
        name=bundle.getString("name");
        usernumber=bundle.getInt("loginnumber");



        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);


        com = (TextView) findViewById(R.id.text_com);
        com.setSelected(true);//首页默认选中
        fav = (TextView) findViewById(R.id.text_fav);
        me = (TextView) findViewById(R.id.text_me);

        FloatingActionButton main_fab = (FloatingActionButton) findViewById(R.id.main_fab);
        main_fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent= new Intent();
                intent.setClass(MainActivity.this,AskActivity.class);
                Bundle askbundle=new Bundle();
                askbundle.putInt("usernumber",usernumber);
                intent.putExtras(askbundle);
                startActivity(intent);
            }
        });
      FragmentManager fm=getFragmentManager();
        fm.beginTransaction()
                .replace(R.id.content_frame,new CommunicationFragment())
                .commit();

        com.setOnClickListener(tabClickListener);
        fav.setOnClickListener(tabClickListener);
        me.setOnClickListener(tabClickListener);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
}

    private View.OnClickListener tabClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() != currentId) {//如果当前选中跟上次选中的一样,不需要处理
                changeSelect(v.getId());//改变图标跟文字颜色的选中
                changeFragment(v.getId());//fragment的切换
                currentId = v.getId();//设置选中id
            }
        }
    };

    private void changeSelect(int resId) {
        com.setSelected(false);
        fav.setSelected(false);
        me.setSelected(false);

        switch (resId) {
            case R.id.text_com:
                com.setSelected(true);
                break;
            case R.id.text_fav:
                fav.setSelected(true);
                break;
            case R.id.text_me:
                me.setSelected(true);
                break;
            }
    }

    private void changeFragment(int resId) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();//开启一个Fragment事务

        hideFragments(transaction);//隐藏所有fragment
        if(resId==R.id.text_com)
        {//主页
                communicationFragment = new CommunicationFragment();
                transaction.replace(R.id.content_frame,communicationFragment);
        }
        else if(resId==R.id.text_fav)
        {//动态
                favorFragment = new FavorFragment();
                Bundle fragmentbundle=new Bundle();
                fragmentbundle.putString("person",name);
                fragmentbundle.putInt("loginnumber",usernumber);
                favorFragment.setArguments(fragmentbundle);
                transaction.replace(R.id.content_frame,favorFragment);
        }
        else if(resId==R.id.text_me)
        {//我

                meFragment = new MeFragment();
                Bundle fragmentbundle=new Bundle();
                fragmentbundle.putString("person",name);
                fragmentbundle.putInt("loginnumber",usernumber);
                meFragment.setArguments(fragmentbundle);
                transaction.replace(R.id.content_frame,meFragment);
        }
        transaction.commit();//一定要记得提交事务
    }

    private void hideFragments(FragmentTransaction transaction){
        if (communicationFragment != null)//不为空才隐藏,如果不判断第一次会有空指针异常
            transaction.hide(communicationFragment);
        if (favorFragment != null)
            transaction.hide(favorFragment);
        if (meFragment != null)
            transaction.hide(meFragment);
        }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
